package com.dk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dk.mapper.QcIqcMapper;
import com.dk.pojo.*;
import com.dk.result.PageResult;
import com.dk.result.Result;
import com.dk.result.ResultConstant;
import com.dk.service.IqcService;
import com.dk.utils.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author LTZ
 * @create 2023/5/11
 */
@Service
public class IqcServiceImpl implements IqcService {

    @Autowired
    private QcIqcMapper iqcMapper;
    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public PageResult<QcIqc> getIqcInfo(Integer pageNum, Integer pageSize, String iqcCode, String vendorCode, String vendorBatch, String itemCode, String checkResult) {
        QcIqcExample example = new QcIqcExample();
        if(iqcCode != null && iqcCode.length() > 0){
            example.createCriteria().andIqcCodeEqualTo(iqcCode);
        }
        if(vendorCode != null && vendorCode.length() > 0){
            example.createCriteria().andVendorCodeEqualTo(vendorCode);
        }
        if(vendorBatch != null && vendorBatch.length() > 0){
            example.createCriteria().andVendorBatchEqualTo(vendorBatch);
        }
        if(itemCode != null && itemCode.length() > 0){
            example.createCriteria().andItemCodeEqualTo(itemCode);
        }
        if(checkResult != null && checkResult.length() > 0){
            example.createCriteria().andCheckResultEqualTo(checkResult);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<QcIqc> qcIqcs = iqcMapper.selectByExample(example);
        PageInfo<QcIqc> pb = new PageInfo<>(qcIqcs);
        PageResult<QcIqc> result = new PageResult<>(pb.getPageNum(), pb.getPageSize(), pb.getTotal(),pb.getList());
        return result;
    }

    @Override
    public Result addIqc(QcIqc iqc, MdVendor vendor, MdItem item, String token) {
        try {
            Claims claims = jwtUtil.parseJwt(token);
            String subject = claims.getSubject();
            User user = JSONObject.parseObject(subject, User.class);
            iqc.setCreateBy(user.getUsername());
            iqc.setCreateTime(new Date());
            iqc.setItemId(item.getItemId());
            iqc.setItemName(item.getItemName());
            iqc.setSpecification(item.getSpecification());
            iqc.setUnitOfMeasure(item.getUnitOfMeasure());
            iqc.setVendorId(vendor.getVendorId());
            iqc.setVendorName(vendor.getVendorName());
            iqc.setVendorNick(vendor.getVendorNick());
            iqcMapper.insertSelective(iqc);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    private void extracted(String token, QcIqc iqc) {
        Claims claims = jwtUtil.parseJwt(token);
        String subject = claims.getSubject();
        User user = JSONObject.parseObject(subject, User.class);
        iqc.setUpdateBy(user.getUsername());
        iqc.setUpdateTime(new Date());
    }

    @Override
    public Result updateIqc(QcIqc iqc, MdVendor vendor, MdItem item, String token) {
        try {
            extracted(token, iqc);
            iqc.setItemId(item.getItemId());
            iqc.setItemName(item.getItemName());
            iqc.setSpecification(item.getSpecification());
            iqc.setUnitOfMeasure(item.getUnitOfMeasure());
            iqc.setVendorId(vendor.getVendorId());
            iqc.setVendorName(vendor.getVendorName());
            iqc.setVendorNick(vendor.getVendorNick());
            iqcMapper.updateByPrimaryKeySelective(iqc);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public Result updateIqc(QcIqc iqc, MdVendor vendor, String token) {
        try {
            extracted(token, iqc);
            iqc.setVendorId(vendor.getVendorId());
            iqc.setVendorName(vendor.getVendorName());
            iqc.setVendorNick(vendor.getVendorNick());
            iqcMapper.updateByPrimaryKeySelective(iqc);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public Result updateIqc(QcIqc iqc, MdItem item, String token) {
        try {
            extracted(token, iqc);
            iqc.setItemId(item.getItemId());
            iqc.setItemName(item.getItemName());
            iqc.setSpecification(item.getSpecification());
            iqc.setUnitOfMeasure(item.getUnitOfMeasure());
            iqcMapper.updateByPrimaryKeySelective(iqc);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public Result updateIqc(QcIqc iqc, String token) {
        try {
            extracted(token, iqc);
            iqcMapper.updateByPrimaryKeySelective(iqc);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public void deleteIqc(Long iqcId) {
        iqcMapper.deleteByPrimaryKey(iqcId);
    }

    @Override
    public QcIqc selectByIqcCode(String iqcCode) {
        QcIqcExample example = new QcIqcExample();
        example.createCriteria().andIqcCodeEqualTo(iqcCode);
        List<QcIqc> qcIqcs = iqcMapper.selectByExample(example);
        return qcIqcs.size() > 0 ? qcIqcs.get(0): null;
    }
}